package com.dotc.kotlinmvvmdemo.NewFragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.dotc.kotlinmvvmdemo.R
import com.dotc.kotlinmvvmdemo.databinding.FragmentMainBinding
import com.dotc.kotlinmvvmdemo.viewModel.MainFragmentViewModel

/**
 *
 */
class MainFragment : Fragment(){
    private val viewModel: MainFragmentViewModel by lazy {
        ViewModelProvider(this,
            ViewModelProvider.AndroidViewModelFactory(requireActivity().application)).get(
            MainFragmentViewModel::class.java)
    }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return DataBindingUtil.inflate<FragmentMainBinding>(inflater,
            R.layout.fragment_main,container,false).let {
            it.lifecycleOwner = this@MainFragment
            //viewModel布局里的名字
            it.viewModel = viewModel
            it.root
        }
    }
}